package upgrade

import (
	"context"

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/sagoo-cloud/sagooiot/api/v1/upgrade"
	"github.com/sagoo-cloud/sagooiot/internal/model"
	"github.com/sagoo-cloud/sagooiot/internal/service"
)

var DevOtaDetail = cUpgradeDev_ota_detail{}

type cUpgradeDev_ota_detail struct{}

// GetDevOtaDetailList 获取列表
func (u *cUpgradeDev_ota_detail) GetDevOtaDetailList(ctx context.Context, req *upgrade.GetDevOtaDetailListReq) (res *upgrade.GetDevOtaDetailListRes, err error) {
	var reqData = new(model.GetDevOtaDetailListInput)
	gconv.Scan(req, &reqData)
	total, currentPage, dataList, err := service.UpgradeDevOtaDetail().GetDevOtaDetailList(ctx, reqData)
	res = new(upgrade.GetDevOtaDetailListRes)
	gconv.Scan(dataList, &res.Data)
	res.PaginationRes.Total = total
	res.PaginationRes.CurrentPage = currentPage
	return
}

// GetDevOtaDetailById 获取指定ID数据
func (u *cUpgradeDev_ota_detail) GetDevOtaDetailById(ctx context.Context, req *upgrade.GetDevOtaDetailByIdReq) (res *upgrade.GetDevOtaDetailByIdRes, err error) {
	data, err := service.UpgradeDevOtaDetail().GetDevOtaDetailById(ctx, req.Id)
	res = new(upgrade.GetDevOtaDetailByIdRes)
	gconv.Scan(data, &res)
	return
}

// AddDevOtaDetail 添加数据
func (u *cUpgradeDev_ota_detail) AddDevOtaDetail(ctx context.Context, req *upgrade.AddDevOtaDetailReq) (res *upgrade.AddDevOtaDetailRes, err error) {
	userInfo := service.Context().GetLoginUser(ctx)
	if userInfo == nil {
		err = gerror.New("未登录或TOKEN失效,请重新登录")
		return
	}
	var data = model.DevOtaDetailAddInput{}
	gconv.Scan(req, &data)
	// data.CreatedBy = userInfo.Id
	err = service.UpgradeDevOtaDetail().AddDevOtaDetail(ctx, data)
	return
}

// EditDevOtaDetail 修改数据
func (u *cUpgradeDev_ota_detail) EditDevOtaDetail(ctx context.Context, req *upgrade.EditDevOtaDetailReq) (res *upgrade.EditDevOtaDetailRes, err error) {
	userInfo := service.Context().GetLoginUser(ctx)
	if userInfo == nil {
		err = gerror.New("未登录或TOKEN失效,请重新登录")
		return
	}
	var data = model.DevOtaDetailEditInput{}
	gconv.Scan(req, &data)
	// data.UpdateBy = userInfo.Id //如果需要保存信息，把这个打开
	err = service.UpgradeDevOtaDetail().EditDevOtaDetail(ctx, data)
	return
}

// DeleteDevOtaDetail 删除数据
func (u *cUpgradeDev_ota_detail) DeleteDevOtaDetail(ctx context.Context, req *upgrade.DeleteDevOtaDetailReq) (res *upgrade.DeleteDevOtaDetailRes, err error) {
	if len(req.Ids) == 0 {
		err = gerror.New("ID参数错误")
	}
	err = service.UpgradeDevOtaDetail().DeleteDevOtaDetail(ctx, req.Ids)
	return
}
